import java.util.*;


class PrimeNumbers {
    private int[] primeNumbers; //array for storing calculated prime numbers
    private int inputNumber;    // initialy entered value
    
    public PrimeNumbers(int inputNumber) {
        this.primeNumbers = new int[inputNumber]; // we can't predict the size of array but it definitelly can't be more than inpunNumber
        this.inputNumber = inputNumber;
    }
    
    private void printPrimeNumbers() {
        if (primeNumbers.length != 0 ) {		
            System.out.println("Prime numbers for " + inputNumber + " are:" );
            for (int i = 0; i < primeNumbers.length; i++) {
                if (primeNumbers[i] == 0) break;        // stop printing after all significant elements are printed
                System.out.print(primeNumbers[i] + "  ");
            }
        } else {
            System.out.println("There are no prime numbers less than entered value!");
        }
    }
    
    private void calculatePrimeNumbers() {
        int primeNumbersCount = 0;
        boolean isPrime = false;
        for (int i = inputNumber; i > 1; i--){
            for (int j = i - 1; j > 1; j--) {
                if (i % j == 0) {
                    isPrime = false;
                    break; 
                } else {
                    isPrime = true;
                }
            }
            if (isPrime) {
                primeNumbers[primeNumbersCount] = i;
                primeNumbersCount++;
            }
        }
    }
    
    private static int readInputNumber(Scanner in) {
        System.out.print("Enter a positive integer number to find prime numbers for: ");
        boolean isFound = false;
        while (!isFound) {
            if (!in.hasNextInt()) {
                System.out.print("Invalid input. Please enter a number: ");
                in.next();
            } else {
                isFound = true;
            }
        }
        int result = in.nextInt();
        return result;
    }
    
    private static boolean isPositive(int number) {
        if (number <= 0) {
            System.out.println("Invalid input: the number must be positive");
            return false;
        } else {
            return true;
        }
    }
    
    public static void main(String[] args) {
		Scanner in = new Scanner(System.in);
        int inputNumberTemp;                    // temp variable to store entered number until a PrimeNumbers object is created
		while (true) {
            inputNumberTemp = readInputNumber(in);
            if (!isPositive(inputNumberTemp)) {
                continue;
            }
            PrimeNumbers primeNumbersObject = new PrimeNumbers(inputNumberTemp);
            primeNumbersObject.calculatePrimeNumbers();
            primeNumbersObject.printPrimeNumbers();
			
            System.out.println("\nPress any digit to continue or '1' to exit");
            if (!in.hasNextInt()) {
                System.out.println("Invalid input!");
                in.close();
                return;
            }
            if (in.nextInt() == 1) {
                in.close();
                return;
            }
		}
	}
}